<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 encoding=utf-8: */
/* $Id: */
/*
Copyright (c) 2010, Justin Swanhart
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

class MySQLDAL implements SimpleDALinterface
{
    #if you want to plug in your own DAL, do so here
    #these functions just wrap the MySQL_perf::mysql_ functions.  This works great if you are using mysqlnd.
    
    private $conn = null;
    private $stmt = null;
    
    function __construct($server = null, $connect = true, $force_new = true)
    {
        if ($connect)
            $this->my_connect($server, $force_new);
    }
    
    function my_insert_id($conn = null)
    {
        if (isset($conn))
            return mysql_insert_id($conn);
        if ($this->conn)
            return mysql_insert_id($this->conn);
        return mysql_insert_id($this->conn);
    }
    
    function my_connect($server = null, $force_new = true)
    {
        if (!$server)
            return false;
        
        if (empty($server['port'])) {
            $port = 3306;
        } else {
            $port = $server['port'];
        }
        
        if (empty($server['user']))
            $server['user'] = 'root';
        if (empty($server['password']))
            $server['password'] = '';
        
        if (!empty($server['options'])) {
            $conn = MySQL_perf::mysql_connect($server['host'] . ':' . $port, $server['user'], $server['password'], $force_new, $server['options']);
        } else {
            $conn = MySQL_perf::mysql_connect($server['host'] . ':' . $port, $server['user'], $server['password'], $force_new);
        }
        
        #if(!empty($server['db'])) mysql_select_db($server['db']);
        $this->conn = $conn;
        return $conn;
    }
    
    function get_warnings($conn)
    {
        $sql      = "show warnings";
        $result   = false;
        $warnings = array();
        if (isset($conn))
            $result = $this->my_query($sql, $conn);
        if ($this->conn)
            $result = $this->my_query($sql, $this->conn);
        while ($row = $this->my_fetch_assoc($result)) {
            $warnings[] = $row;
            
        }
        return $warnings;
        
    }
    
    function my_affected_rows($conn = null)
    {
        if (isset($conn)) {
            return (mysql_affected_rows($conn));
        } elseif ($this->conn) {
            return (mysql_affected_rows($this->conn));
        }
        return (mysql_affected_rows());
        
    }
    
    function my_query($sql = '', $conn = null)
    {
        if (isset($conn)) {
            $this->stmt = MySQL_perf::mysql_query($sql, $conn);
        } elseif ($this->conn) {
            $this->stmt = MySQL_perf::mysql_query($sql, $this->conn);
        } else {
            $this->stmt = MySQL_perf::mysql_query($sql);
        }
        return $this->stmt;
    }
    
    function my_unbuffered_query($sql, $conn = null)
    {
        if (isset($conn)) {
            $this->stmt = MySQL_perf::mysql_unbuffered_query($sql, $conn);
        } elseif ($this->conn) {
            $this->stmt = MySQL_perf::mysql_unbuffered_query($sql, $this->conn);
        } else {
            $this->stmt = MySQL_perf::mysql_unbuffered_query($sql);
        }
        return $this->stmt;
    }
    
    function my_fetch_assoc($stmt = null)
    {
        if (!$stmt)
            $stmt = $this->stmt;
        return mysql_fetch_assoc($stmt);
    }
    
    function my_fetch_array($stmt = null)
    {
        if (!$stmt)
            $stmt = $this->stmt;
        return mysql_fetch_array($stmt);
    }
    
    function my_errno($conn = null)
    {
        if ($conn) {
            return mysql_errno($conn);
        }
        if ($this->conn)
            return mysql_errno($this->conn);
        
        return mysql_errno();
    }
    
    function my_error($conn = null)
    {
        if (isset($conn)) {
            return mysql_error($conn);
        }
        if ($this->conn)
            return mysql_error($this->conn);
        
        return mysql_error();
    }
    
    function my_select_db($db, $conn = null)
    {
        if (isset($conn))
            return mysql_select_db($db, $conn);
        if ($this->conn)
            return mysql_select_db($db, $this->conn);
        
        return mysql_select_db($db);
    }
    
    function my_field_type($result = null, $field_offset = 0)
    {
        if (!isset($result))
            $result = $this->stmt;
        return mysql_field_type($result, $field_offset);
    }
    
    function my_free_result($stmt = null)
    {
        if ($stmt)
            return mysql_free_result($stmt);
        if ($this->stmt)
            return mysql_free_result($this->stmt);
        return true;
    }
    
    function my_close($conn = null)
    {
        if ($conn)
            return mysql_close($conn);
        if ($this->conn)
            return mysql_close($this->conn);
        return false;
    }
    
    function my_begin($conn = null)
    {
        if (!$conn) {
            return MySQL_perf::mysql_query('begin', $this->conn);
        } else {
            return MySQL_perf::mysql_query('begin', $conn);
        }
    }
    
    function my_autocommit($state = true, $conn = null)
    {
        if ($state)
            $state = 'true';
        else
            $state = 'false';
        if (!$conn) {
            return MySQL_perf::mysql_query("set autocommit=$state", $this->conn);
        } else {
            return MySQL_perf::mysql_query("set autocommit=$state", $conn);
        }
    }
    
    function my_commit($conn = null)
    {
        if ($conn) {
            return MySQL_perf::mysql_query('commit', $conn);
        } else {
            return MySQL_perf::mysql_query('commit', $this->conn);
        }
    }
    
    function my_rollback($conn = null)
    {
        if ($conn) {
            return MySQL_perf::mysql_query('rollback', $conn);
        } else {
            return MySQL_perf::mysql_query('rollback', $this->conn);
        }
    }
    
    function my_real_escape_string($string, $conn = null)
    {
        if ($conn != null) {
            return mysql_real_escape_string($string, $conn);
        }
        if ($this->conn)
            return mysql_real_escape_string($string, $this->conn);
        return mysql_real_escape_string($string);
    }
    
    function my_escape_string($string, $conn = null)
    {
        return $this->my_real_escape_string($string, $conn);
    }
    
    
    #END DAL
}
